package org.cem.security;

import java.net.URL;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.eclipse.equinox.security.auth.ILoginContext;
import org.eclipse.equinox.security.auth.ILoginContextListener;
import org.eclipse.equinox.security.auth.LoginContextFactory;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wb.swt.BrowserUtil;

public class RapLoginContext implements ILoginContext {

	private String name;
	private URL conf;
	
	private SourceProviderListener sourceProviderListener;

	public RapLoginContext(String name, URL conf) throws LoginException {
		this.name = name;
		this.conf = conf;
		this.sourceProviderListener = new SourceProviderListener();
	}

	public Subject getSubject() throws LoginException {
		return getSessionScopeLoginContext().getSubject();
	}

	public void login() throws LoginException {
		getSessionScopeLoginContext().login();
	}

	public void logout() throws LoginException {
		getSessionScopeLoginContext().logout();
	}

	public LoginContext getLoginContext() throws LoginException {
		return getSessionScopeLoginContext().getLoginContext();
	}

	public void registerListener(ILoginContextListener listener) {
		getSessionScopeLoginContext().registerListener(listener);
	}

	public void unregisterListener(ILoginContextListener listener) {
		getSessionScopeLoginContext().unregisterListener(listener);
	}

	private ILoginContext getSessionScopeLoginContext() {

		ILoginContext loginContext = (ILoginContext) RWT.getUISession()
				.getAttribute(ILoginContext.class.getName());
		if (loginContext == null) {
			loginContext = LoginContextFactory.createContext(name, conf, new DialogCallbackHandler());
			loginContext.registerListener(sourceProviderListener);
			RWT.getUISession().setAttribute(ILoginContext.class.getName(),
					loginContext);
		}
		return loginContext;
	}
	
	private class SourceProviderListener implements ILoginContextListener {

		public void onLoginFinish(Subject subject, LoginException loginException) {
	
			if (loginException != null) {
				return;
			}
			
		}

		public void onLoginStart(Subject subject) {
		}

		public void onLogoutFinish(Subject subject,
				LoginException logoutException) {
			
//			try {
//				PlatformUI.getWorkbench().
//				PlatformUI.getWorkbench().close();
//			} catch (IllegalStateException e) {
//				
//			}
			BrowserUtil.openURL(BrowserUtil.getCurrentBrowserURL());
		}

		public void onLogoutStart(Subject subject) {
		}
		
	} 
}
